import { 
  queryData, 
  querySignals,
  queryInfo,
  settingMoney,
  buyOrSale,
  queryName,
  ignore
} from "../services/indexPage";
import { notification } from 'antd';
import { routerRedux } from 'dva/router';
let Interval = null;
export default {
  namespace : "indexPage",
  state : {
    stockholds : [],
    info : {},
    signals : [],
    SettingShow : false,
    isPhone : document.body.clientWidth < 760,
    loading : false,
    buyShow : false,
    modalOpt : {},
    name : ""
  },
  subscriptions : {
    setup({ dispatch, history }){
      history.listen(({ pathname })=>{
        if(pathname === "/home" ){
          dispatch({
            type : "init"
          })
          clearInterval(Interval)
          Interval = setInterval(()=>{
            dispatch({ type : "init" })
          },10000)
        }else {
          clearInterval(Interval)
        }
      })
      let rs = null;
      window.addEventListener("resize",(e)=>{
        clearTimeout(rs)
        rs = setTimeout(()=>{
            dispatch({
              type : "changeLayout" 
            })
          },300)
        }
      )
      
    }
  },
  effects : {
    *init(action, { call, put }){
      yield put({ type :"showLoading" })
      const { data: stock } = yield call(queryData)
      if(stock){
        yield put({ type : "queryDataSuccess", payload : { stock } })
      }
      const { data : info } = yield call(queryInfo)
      if(info){
        yield put({ type : "queryInfoSuccess", payload : { info } })
      }
      const { data : signals } = yield call(querySignals)
      if(signals){
        yield put({ type : "querySignalsSuccess", payload : { signals } })
      }
      const { data : n } = yield call(queryName)
      if(n){
        yield put({ type : "queryNameSuccess", payload : n })
      }
      yield put({ type :"hideLoading" })
    },
    *changeLayout(action, { put, select }){
      const { indexPage } = yield (select(_=>_));
      const isPhone = document.body.clientWidth < 760;
      if(isPhone !== indexPage.isPhone){
        yield put({
          type : "handleLayout",
          payload : isPhone
        })
      }
    },
    *settingMoney({ payload }, { put, call }){
      const { data: m } = yield call(settingMoney,payload)
      if(m.code === 201){
        notification.success({
          description : m.info,
          message : "修改金额"
        })
        yield put({ type : "hideModal" })
        yield put(routerRedux.replace("/home"))
      }else if(m.code === 300){
        notification.error({
          description : m.info,
          message : "买入/卖出"
        })
      }
    },
    *querysignals({ payload }, { put, call }){
      yield put({ type : "showLoading" })
      const { data : signals } = yield call(querySignals)
      if(signals){
        yield put({ type : "querysignalsSuccess", payload : signals })
      }
      yield put({type : "hideLoading" })
    },
    *buyOrSale({ payload }, { call, put }){
      const { data: b } = yield call(buyOrSale, payload);
      yield put({ type : "hideBuyModal" })
      if(b.code === 201){
        notification.success({
          description : b.info,
          message : "买入/卖出"
        })
        yield put(routerRedux.replace("/"))
      }else if(b.code === 300){
        notification.error({
          description : b.info,
          message : "买入/卖出"
        })
      }
    },
    *ignore({ payload }, { call, put }){
      const { data: b } = yield call(ignore, payload);
      yield put({ type : "hideBuyModal" })
      if(b.code === 201){
        notification.success({
          description : b.info,
          message : "忽略策略信号"
        })
        yield put(routerRedux.replace("/"))
      }else if(b.code === 300){
        notification.error({
          description : b.info,
          message : "忽略策略信号"
        })
      }
    }
  },
  reducers : {
    initSuccess(state,{ payload }){
      return {
        ...state,
        stockholds : payload.stock,
        info : payload.info,
        signals : payload.signals
      }
    },
    querysignalsSuccess(state,{ payload }){
      return {
        ...state,
        signals : payload
      }
    },
    queryDataSuccess(state, { payload }) {
      return {
        ...state,
        stockholds : payload.stock
      }
    },
    queryInfoSuccess(state, { payload }){
      return {
        ...state,
        info : payload.info
      }
    },
    querySignalsSuccess(state, { payload }) {
      return {
        ...state,
        signals : payload.signals
      }
    },
    queryNameSuccess(state, { payload }){
      return {
        ...state,
        name : payload.name
      }
    },
    handleLayout(state, { payload }) {
      return {
        ...state,
        isPhone : payload
      }
    },
    showModal(state){
      return {
        ...state,
        SettingShow : true
      }
    },
    hideModal(state){
      return {
        ...state,
        SettingShow : false
      }
    },
    showLoading(state){
      return {
        ...state,
        loading : true
      }
    },
    hideLoading(state){
      return {
        ...state,
        loading : false
      }
    },
    showBuyModal(state, { payload }) {
      return {
        ...state,
        buyShow : true,
        modalOpt : payload
      }
    },
    hideBuyModal(state, { payload }) {
      return {
        ...state,
        buyShow : false
      }
    }
  }
}